package taskManagerIncrement3TEST;

import static org.junit.Assert.*;      

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import taskManagerSystem.ModelSubsystem.Task;



public class IOHandlerTEST1 {

	Task task;
	Vector<Task> tasks = new Vector<Task>();
	
	@Test
	public void testLoadTasksFile() {
		
		Vector<Task> tasks = new Vector<Task>();
		
		File file = new File("tasks.xml");
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db;
		try {
			db = dbf.newDocumentBuilder();
			Document doc = db.parse(file);
			doc.getDocumentElement().normalize();
			NodeList taskLst = doc.getElementsByTagName("Task");
	
				Task task; 
				Node taskNode = taskLst.item(0);
				Element taskElem = (Element) taskNode;
				
				Element taskIDE      = (Element) taskElem.getElementsByTagName("TaskID").item(0);
				Element titleE       = (Element) taskElem.getElementsByTagName("Title").item(0);
				Element descriptionE = (Element) taskElem.getElementsByTagName("Description").item(0);
				Element durationE    = (Element) taskElem.getElementsByTagName("Duration").item(0);
				Element deliverableE = (Element) taskElem.getElementsByTagName("Deliverable").item(0);
				Element deadlineE    = (Element) taskElem.getElementsByTagName("DeadlineDate").item(0);
				Element personE      = (Element) taskElem.getElementsByTagName("Person").item(0);
				Element parentTaskIDE= (Element) taskElem.getElementsByTagName("ParentTaskID").item(0);
				Element prerequisiteTaskIDE = (Element) taskElem.getElementsByTagName("PrerequisiteTask").item(0);
				
				String taskID 		= taskIDE.getChildNodes().item(0).getNodeValue();
				String title 		= titleE.getChildNodes().item(0).getNodeValue();
				String description	= descriptionE.getChildNodes().item(0).getNodeValue();
				String duration		= durationE.getChildNodes().item(0).getNodeValue();
				String deliverable 	= deliverableE.getChildNodes().item(0).getNodeValue();
				String deadline 	= deadlineE.getChildNodes().item(0).getNodeValue();
				String person 		= personE.getChildNodes().item(0).getNodeValue();
				int parentTaskID    = Integer.parseInt(parentTaskIDE.getChildNodes().item(0).getNodeValue());
				int prerequisiteTaskID    = Integer.parseInt(prerequisiteTaskIDE.getChildNodes().item(0).getNodeValue());

				task = new Task(Integer.parseInt(taskID), title, description, Integer.parseInt(duration),
						deliverable, deadline, person, parentTaskID, prerequisiteTaskID);
				tasks.add(task);
				
				assertEquals("1",taskID);
				assertEquals("Task1",title);
				assertEquals("Description1",description);
				assertEquals("10",duration);
				assertEquals("Testing",deliverable);
				assertEquals("1/10/2012",deadline);
				assertEquals("Scott",person);
				assertEquals(0, parentTaskID);
				assertEquals(0, prerequisiteTaskID);
				
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}

	@Test
	public void testSaveTaskFile() {
		
		File file = new File("tester.xml");
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db;
		
		PrintWriter outputStream = null;
		try
		{
			outputStream = new PrintWriter(new FileOutputStream(file));
		}
		catch(FileNotFoundException e)
		{
			System.exit(0);
		}
		
		outputStream.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		outputStream.println("<Tasks>");

		
			//Task task = taskList.get(i);
			outputStream.println("\t<Task>");
			outputStream.println("\t\t<TaskID>" + "TaskID1" + "</TaskID>");
			outputStream.println("\t\t<Title>" + "Title1" + "</Title>");
			outputStream.println("\t\t<Description>" + "Description1" + "</Description>");
			outputStream.println("\t\t<Duration>" + "Duration1" + "</Duration>");
			outputStream.println("\t\t<Deliverable>" + "Deliverable1" + "</Deliverable>");
			outputStream.println("\t\t<DeadlineDate>" + "DeadlineDate1" + "</DeadlineDate>");
			outputStream.println("\t\t<Person>" + "Person1" + "</Person>");
			outputStream.println("\t\t<ParentTaskID>" + "1" + "</ParentTaskID>");
			outputStream.println("\t\t<PrerequisiteTask>" + "1" + "</PrerequisiteTask>");
			outputStream.println("\t</Task>");
		 
		
		outputStream.println("</Tasks>");
		outputStream.close();
		try {
			db = dbf.newDocumentBuilder();
			Document doc = db.parse(file);
			doc.getDocumentElement().normalize();
			NodeList taskLst = doc.getElementsByTagName("Task");
			
				Node taskNode = taskLst.item(0);
				Element taskElem = (Element) taskNode;
				
				Element taskIDE      = (Element) taskElem.getElementsByTagName("TaskID").item(0);
				Element titleE       = (Element) taskElem.getElementsByTagName("Title").item(0);
				Element descriptionE = (Element) taskElem.getElementsByTagName("Description").item(0);
				Element durationE    = (Element) taskElem.getElementsByTagName("Duration").item(0);
				Element deliverableE = (Element) taskElem.getElementsByTagName("Deliverable").item(0);
				Element deadlineE    = (Element) taskElem.getElementsByTagName("DeadlineDate").item(0);
				Element personE      = (Element) taskElem.getElementsByTagName("Person").item(0);
				Element parentTaskIDE= (Element) taskElem.getElementsByTagName("ParentTaskID").item(0);
				Element prerequisiteTaskIDE = (Element) taskElem.getElementsByTagName("PrerequisiteTask").item(0);
				
				String taskID 		= taskIDE.getChildNodes().item(0).getNodeValue();
				String title 		= titleE.getChildNodes().item(0).getNodeValue();
				String description	= descriptionE.getChildNodes().item(0).getNodeValue();
				String duration		= durationE.getChildNodes().item(0).getNodeValue();
				String deliverable 	= deliverableE.getChildNodes().item(0).getNodeValue();
				String deadline 	= deadlineE.getChildNodes().item(0).getNodeValue();
				String person 		= personE.getChildNodes().item(0).getNodeValue();
				int parentTaskID    = Integer.parseInt(parentTaskIDE.getChildNodes().item(0).getNodeValue());
				int prerequisiteTaskID    = Integer.parseInt(prerequisiteTaskIDE.getChildNodes().item(0).getNodeValue());
		
				assertEquals("TaskID1",taskID);
				assertEquals("Title1",title);
				assertEquals("Description1",description);
				assertEquals("Duration1",duration);
				assertEquals("Deliverable1",deliverable);
				assertEquals("DeadlineDate1",deadline);
				assertEquals("Person1",person);
				assertEquals(1, parentTaskID);
				assertEquals(1, prerequisiteTaskID);
				
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		//because outputStream.close() is not in the code, the task never saves, 
		//therefore the test can be run multiple times without the task descriptions saving
		//for the next test run
		//outputStream.close();
	}
	
	

	@Test
	public void testSavePersonFile() {
		
		PrintWriter outputStream = null;
		try
		{
			outputStream = new PrintWriter(new FileOutputStream("Ptester.txt"));
		}
		catch(FileNotFoundException e)
		{
			System.exit(0);
		}

		outputStream.println("Scott, 32 hours, Task1 & Task8 & Task11");
		try{
		FileInputStream fstream = new FileInputStream("Ptester.txt");
		DataInputStream in = new DataInputStream(fstream);
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		  String strLine;
		  //Read File Line By Line
		  while ((strLine = br.readLine()) != null)   {

			  assertEquals("Scott, 32 hours, Task1 & Task8 & Task11", strLine);
		  	}	
		in.close();
			}
		catch (Exception e)
		{								//Catch exception if any
			  System.err.println("Error: " + e.getMessage());
		}

		//because outputStream.close() is not in the code, the task never saves, 
		//therefore the test can be run multiple times without the task descriptions saving
		//for the next test run
		//outputStream.close();
	}
}
